package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.KnowledgeRoom;
import com.xiaochong.tir.common.data.vo.KnowledgeRoomVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.KnowledgeRoomService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/3 10:35
 */
@RestController
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"kroom/")
@Api("知识讲堂管理")
public class KnowledgeRoomController {

    @Autowired
    private KnowledgeRoomService knowledgeRoomService;

    @Autowired
    private OSSTool ossTool;
    @Autowired
    private RedisServer redisServer;

    /**
     * 知识讲堂管理页面
     * @return
     */
    @GetMapping("klroom")
    private Object knRoomPage(){
        return new ModelAndView("knowledgeroom/klroomPage");
    }

    /**
     * 查询知识讲堂列表
     * @param vo
     * @return
     */
    @ApiOperation(value = "查询知识讲堂列表",notes = "查询知识讲堂列表")
    @PostMapping("klroomList")
    public Object knowledgeRoomList(KnowledgeRoomVo vo){
        PageInfo<KnowledgeRoom> info = knowledgeRoomService.selectKlroomList(vo);
        return new ReturnResult<KnowledgeRoom>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 修改知识讲堂状态
     * @param knowledgeRoom
     * @return
     */
    @ApiOperation(value = "修改知识讲堂状态",notes = "修改知识讲堂状态")
    @PostMapping("enableKlroom")
    public Object enableKlroom(KnowledgeRoom knowledgeRoom){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改知识讲堂状态");
        KnowledgeRoom old =  knowledgeRoomService.selectByPrimaryKey(knowledgeRoom.getId());
        if(StringUtils.isNotBlank(String.valueOf(knowledgeRoom.getEnableFlag()))&&knowledgeRoom.getEnableFlag()){
            knowledgeRoom.setEnableFlag(false);
        }else {
            knowledgeRoom.setEnableFlag(true);
        }
        knowledgeRoomService.updateByKlroomKey(knowledgeRoom,old);
        return new ReturnResult<KnowledgeRoom>(0,"成功",1,1);
    }

    /**
     * 知识讲堂置顶
     * @param knowledgeRoom
     * @return
     */
    @ApiOperation(value = "知识讲堂置顶",notes = "知识讲堂置顶")
    @PostMapping("topKlroom")
    public Object topKlroom(KnowledgeRoom knowledgeRoom){
        redisServer.set(RedisKeysUtils.LOGINNAME,"知识讲堂置顶");
        KnowledgeRoom old =  knowledgeRoomService.selectByPrimaryKey(knowledgeRoom.getId());
        if(StringUtils.isNotBlank(String.valueOf(knowledgeRoom.getSetTop()))&&knowledgeRoom.getSetTop()){
            knowledgeRoom.setSetTop(false);
        }else {
            knowledgeRoom.setSetTop(true);
        }
        knowledgeRoomService.updateByKlroomKey(knowledgeRoom,old);
        return new ReturnResult<KnowledgeRoom>(0,"成功",1,1);
    }

    /**
     * 新增知识讲堂
     * @param knowledgeRoom
     * @return
     */
    @ApiOperation(value = "新增知识讲堂",notes = "新增知识讲堂")
    @PostMapping("insKlroom")
    public Object insKlroom(KnowledgeRoom knowledgeRoom){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增知识讲堂");
        KnowledgeRoom old = knowledgeRoomService.selectByEntity(knowledgeRoom);
        if(null != old){
            return new ReturnResult<KnowledgeRoom>(-1,"重复添加",1,1);
        }
        knowledgeRoom.setCreateTime(new Date());
        if(StringUtils.isNotBlank(String.valueOf(knowledgeRoom.getSetTop()))&&knowledgeRoom.getSetTop()){
            knowledgeRoom.setEnableFlag(true);
        }else{
            knowledgeRoom.setEnableFlag(false);
        }
        if(StringUtils.isNotBlank(knowledgeRoom.getPublishMode())&&knowledgeRoom.getPublishMode().equals("1")){
            knowledgeRoom.setPublishTime(new Date());
        }
        knowledgeRoomService.insertSelective(knowledgeRoom);
        return new ReturnResult<KnowledgeRoom>(0,"成功",1,1);
    }

    /**
     * 新增知识讲堂页面
     * @return
     */
    @GetMapping("addKlroom")
    public Object addKlroom(){
        return new ModelAndView("knowledgeroom/addKlroomPage");
    }

    /**
     * 编辑知识讲堂页面
     * @return
     */
    @GetMapping("editKlroom")
    public Object editKlroom(@RequestParam("id")Integer id){
        KnowledgeRoom knowledgeRoom = knowledgeRoomService.selectByPrimaryKey(id);
        if(StringUtils.isNotBlank(knowledgeRoom.getArticleImg())){
            knowledgeRoom.setArticleImgKey(knowledgeRoom.getArticleImg());
            knowledgeRoom.setArticleImg(ossTool.getOSSUrl(knowledgeRoom.getArticleImgKey()));
        }
        ModelAndView mv = new ModelAndView("knowledgeroom/editKlroomPage");
        mv.addObject("klr",knowledgeRoom);
        return mv;
    }

    /**
     * 编辑知识讲堂
     * @param knowledgeRoom
     * @return
     */
    @ApiOperation(value = "编辑知识讲堂",notes = "编辑知识讲堂")
    @PostMapping("updateKlroom")
    public Object updateKlroom(KnowledgeRoom knowledgeRoom){
        redisServer.set(RedisKeysUtils.LOGINNAME,"编辑知识讲堂");
        KnowledgeRoom old =  knowledgeRoomService.selectByPrimaryKey(knowledgeRoom.getId());
        knowledgeRoom.setEnableFlag(old.getEnableFlag());
        knowledgeRoomService.updateByKlroomKey(knowledgeRoom,old);
        return new ReturnResult<KnowledgeRoom>(0,"成功",1,1);
    }

}
